//必须带done回调 执行完成调用
import { ElMessage } from 'element-plus'
import { translateTitle } from '@/utils/i18n'
import isEmpty from 'lodash/isEmpty'

export default function validateForm({ vm, action, done, eventName }) {
  if (action.extension.formSelect.value.length > 0) {
    // 修改为验证多个表单
    let validFlag = true
    action.extension.formSelect.value.forEach((item) => {
      if (vm.vuiComponents[item].self.getInfo.type == 'uiCirculationForm') {
        // vm.vuiComponents[item].self.getInfo.list[1].list.forEach((row) => {
        //   let ref = vm.vuiComponents[row._id].ref
        //   if (ref && ref.validate) {
        //     ref.validate((valid) => {
        //       if (!valid) {
        //         validFlag = false
        //         ElMessage({
        //           showClose: true,
        //           message: `${translateTitle('Ve.eventTask.表单验证')} - ${vm.vuiComponents[row._id].self.getInfo.name} ${translateTitle(
        //             'Ve.eventTask.验证失败'
        //           )}`,
        //           type: 'error',
        //         })
        //       }
        //     })
        //   }
        // })
        vm.vuiComponents[item].self.$refs.uiCirculationForm_from.validate((valid) => {
          console.log(valid)
          if (!valid) {
            validFlag = false
            ElMessage({
              showClose: true,
              message: `${translateTitle('Ve.eventTask.表单验证')} - ${vm.vuiComponents[item].self.getInfo.name} ${translateTitle(
                'Ve.eventTask.验证失败'
              )}`,
              type: 'error',
            })
            // vm.vuiComponents[item].self.$refs.uiCirculationForm_from.clearValidate()
          }
        })
      } else {
        let ref = vm.vuiComponents[item].ref
        const self = vm.vuiComponents[item].self
        // 生成表单项字段描述映射
        if (isEmpty(self.formItemMap)) {
          Object.keys(self.info.data.formItems).forEach((i) => {
            const formItem = self.vuiComponents[i]
            if (formItem) {
              const label = formItem.self.info.extension.label.value
              const field = formItem.self.info.extension.model.value
              self.formItemMap[field] = label
            }
          })
        }
        let validOpen = false
        if (ref && ref.validate) {
          ref.validate((valid, cacheObj) => {
            if (!valid) {
              validFlag = false
              validOpen = true
              // 失败提示
              Object.keys(cacheObj).forEach((i) => {
                // 放入定时器中 防止多条提示重叠
                setTimeout(() => {
                  ElMessage({
                    showClose: true,
                    message: `${self.formItemMap[i]} - ${cacheObj[i][0].message}`,
                    type: 'error',
                  })
                })
              })
            }
          })
        }
        if (!validOpen && !validateUpload(vm, vm.vuiComponents[item]) && vm.vuiComponents[item].self?.getInfo?.extension.extendRule) {
          validFlag = false
          ElMessage({
            showClose: true,
            message: translateTitle('Ve.eventTask.上传未完成'),
            type: 'error',
          })
        }
      }
    })
    if (validFlag) {
      done(null, {})
    } else {
      // 修改任务队列执行状态，防止下一次事件被拦截
      console.log('xiugaizhuangtai ', vm, eventName)
      vm[`${eventName}IsRunning`] = false
    }
  } else {
    done(null, {})
  }
}
function validateUpload(vm, formInfo) {
  let list = []
  let _upload = true
  let formItems = formInfo.self?.getInfo?.data?.formItems
  for (const key in formItems) {
    const element = formItems[key]
    if (element.indexOf('uiUpload') > -1) {
      list.push(element)
    }
  }
  for (let index = 0; index < list.length; index++) {
    const element = list[index]
    let _self = vm.vuiComponents[element].self
    if (!_self.successOpen && vm.vuiComponents[element].self.getInfo.extension.listType.value != 'picture-card') {
      _upload = false
    }
  }
  return _upload
}
